#include "../GKoSonTools/BLUETOOTH/BM77/bm77.h" 
#include "../GKoSonTools/SYSCFG/sysCfg.h" 
#include "../GKoSonTools/GKUSART/gkusart.h"
#include "../GKoSonTools/UNIT/unit.h"

#include "../GKoSonTools/COMPONENT/KEILcomponent.h"
static void bm77_resert( void );
static char U3STATUS=INTERSIDE;
static char Get_U3status(void){return U3STATUS;};
static void Set_U3status(char a){U3STATUS=a;};
static uint8_t bm77_read_mac(uint8_t *mac);
static void bm77_normal_mode(void);
static void bm77_cmd_mode(void);

static void TXRX_init(void)
{
	 GK_usart_init(&GKU3,3);
}


uint8_t bm77_set_parma(uint16_t cmd , uint8_t *data ,uint8_t length)
{
	uint8_t buffer[128] = {0x00} , cnt = 0;
  uint16_t rxlen=0;
	uint8_t rxbuffer[128] = {0x00};
    
	buffer[cnt++] =  0x01;
	buffer[cnt++] =  0x27;
	buffer[cnt++] =  0xfc;
	buffer[cnt++] =  length+3;
	buffer[cnt++] =  ((cmd>>8)&0xff);
	buffer[cnt++] =  (cmd&0xff);
	buffer[cnt++] =  length;
	memcpy(buffer+cnt , data , length);
  cnt += length;
    
	printf("TX::");
	All_print(buffer,cnt);
	
	if(GK_usart_TXRX(&GKU3,buffer,cnt,rxbuffer,&rxlen))//成功返回
	{
		if(rxbuffer[6]==0x00){printf("BM77 write OK ,CMD =%x \n" , cmd);return 0;}
		else {printf("BM77 write fail ,CMD =%x \n" , cmd);return 1;}
	
	}
	else
	{
		printf("BM77 send fail ,CMD =%x \n" , cmd);return 2;
	}
	
}

uint16_t bm77_read_parma(uint16_t cmd  ,uint8_t *RX,uint16_t length)
{
	uint8_t  buffer[125] = {0} , cnt = 0 ;
	uint16_t reCmd=0,rxlen=0;
	uint8_t rxbuffer[128] = {0x00};
  uint8_t answer=BLE_COMMAND_NOBACK_ERR;
	buffer[cnt++] =  0x01;//固定TYPE
	buffer[cnt++] =  0x29;
	buffer[cnt++] =  0xfc;//固定fild
	buffer[cnt++] =  0x03;
	buffer[cnt++] =  ((cmd>>8)&0xff);
	buffer[cnt++] =  (cmd&0xff);
	buffer[cnt++] =  length;
	printf("TX::");
	All_print(buffer,cnt);
	
	if(GK_usart_TXRX(&GKU3,buffer,cnt,rxbuffer,&rxlen))//成功返回
	{
		printf("RX::");
	  All_print(rxbuffer,20);
		if(rxbuffer[6]!=0x00)
			{
				printf("BM77返回值不可靠\n");
				answer = BLE_COMMAND_NOBACK_ERR;
				return answer;
			}
		else 
		{
				reCmd = rxbuffer[7]<<8|rxbuffer[8];
				if(( reCmd == cmd) && length == rxbuffer[9])
				{
					for(uint8_t i = 0 ; i < length ; i++)
					{
						RX[i] = rxbuffer[10+i];
					}
					answer = BLE_OK;
				  return answer;
				}
				else
				{
					printf("BM77返回值异常\n");
					answer = BLE_COMMAND_NOBACK_ERR;
				  return answer;
				}
		
		}
		
	
	}
	
	else
	{
	  printf("BM77 send fail ,CMD =%x \n" , cmd);return 2;
	}


}






//发送cmd出去 返回respone数组 它是封装函数 真正在上面
//0--成功 其他--失败
uint8_t bm77_read_data(uint16_t cmd , uint8_t *respone ,uint8_t length)
{
    uint8_t cnt = 3 , resault = BLE_INIT_ERR;
    
    while(( cnt-- ) && (resault != BLE_OK))
    {
        resault = bm77_read_parma( cmd , respone ,length );
        if( resault != BLE_OK)
        {
            bm77_resert();//靠它解决了问题！
            HAL_Delay(200);
						printf("$$$$$$$");
        }
    }
    return resault;
}


//0--成功 其他--失败
//static 
	uint8_t bm77_read_mac(uint8_t *mac)
{
	uint8_t tempMac[6] = {0x00};
	uint32_t ver=140;
	if(0==bm77_read_data(BM77_MAC_CMD , tempMac , 6))
	{
	  config.write ( CFG_BLE_MAC ,tempMac ,1);
		config.write ( CFG_BLE_VERSION ,&ver ,0);
		return 0;
	}
	else
		return 1;
}

uint8_t bm77_write_mac(uint8_t *mac)
{
	return bm77_set_parma(BM77_MAC_CMD , mac , 6);
}

uint8_t bm77_read_version(uint8_t *mac)
{
	return 0;
}

uint8_t bm77_write_msg(uint8_t *ptAddr , uint8_t *ptSenddata , uint8_t uLegnth , uint16_t dHandle)
{
	HAL_UART_Transmit(&huart3, ptSenddata, uLegnth, 0xFFFF);
	return BLE_OK;
}
//0--成功 其他--失败
uint8_t bm77_set_default( void )
{
    uint8_t bm77Name[11]={"HELLOKOSON"};
    uint8_t rst=0;
    bm77_cmd_mode();
    rst=bm77_set_parma(BM77_NAME_CMD ,  bm77Name , 11);
    bm77_normal_mode();  
	  return rst;
}
void bm77_force_normal(void)
{
	printf("\n%s\n",__FUNCTION__);
	bm77_normal_mode();
}

/////////////GPIO//////////
static void bm77_resert( void )
{
//复位脚0
//延时200
//复位脚1
//延时
	HAL_GPIO_WritePin(BM77RST_GPIO_Port, BM77RST_Pin, GPIO_PIN_RESET);
	HAL_Delay(200);
	HAL_GPIO_WritePin(BM77RST_GPIO_Port, BM77RST_Pin, GPIO_PIN_SET);
	HAL_Delay(500);//复位以后要多等一会儿 否则交互失败
}

//这个函数逻辑非常深刻 缺一不可
//先rename
//在拉高电平 进入透传模式
//在复位 以后就自动开始广播了 可以APP找到它了
void bm77_normal_mode(void)
{
//控制脚1
//复位一下	bm77_resert();
	HAL_GPIO_WritePin(BM77CNT_GPIO_Port, BM77CNT_Pin, GPIO_PIN_SET);
	bm77_resert();
}
void bm77_cmd_mode(void)
{
//控制脚0
//复位一下	bm77_resert();
	HAL_GPIO_WritePin(BM77CNT_GPIO_Port, BM77CNT_Pin, GPIO_PIN_RESET);
	bm77_resert();
}

void bm77_init(void)
{
//初始化串口3   115200 回调函数
//复位脚out 控制脚out &复位脚1
	printf("%s######\r\n",__FUNCTION__);
	TXRX_init();
	Set_U3status(INTERSIDE);
	HAL_GPIO_WritePin(BM77RST_GPIO_Port, BM77RST_Pin, GPIO_PIN_SET);//高电平 开机
  bm77_cmd_mode();//此后务必进去CMD模式
}

btDrvType	BM77Drv=
{
	.resert = bm77_resert,
	.read_mac = bm77_read_mac,
	.write_mac = bm77_write_mac,
	.read_ver = bm77_read_version,
	.send = bm77_write_msg,
	.set_default = bm77_set_default,
	.force_normal = bm77_force_normal,
};




void bm77_test(void)
{
	static char once=1;
	uint8_t tempMac[6] = {0x00};
	
	if(once)
	{
		 once=0;
	   bm77_init();
		 printf("for once bm77_init!/r/n");
	}
	if(Get_U3status()==INTERSIDE)
	{
		if(bm77_read_mac(tempMac)==BLE_OK)
		 Set_U3status(OUTERSIDE);
	}
	else if(Get_U3status()==OUTERSIDE)
	{
		bm77_set_default();
		Set_U3status(RADIO);
	}
	else if(Get_U3status()==RADIO)
	{
		//printf("I AM OK\n");
	}

}


INIT_EXPORT("bm77_init",bm77_init); 
